package com.lijie.bs_recruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.uploadUtil;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.Company;
import com.lijie.bs_recruit.service.CompanyService;
import com.lijie.bs_recruit.service.HrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Classname CompanyController
 * @Description TODO
 * @Date 2021/4/2 14:29
 * @Created by lijie
 */
@RestController()
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    CompanyService companyService;
    @Autowired
    HrService hrService;
    @Autowired
    uploadUtil uploadUtil;


    @Value("${web.company-path}")
    private String path;
    //增加
    @RequestMapping("/addCompany/{hrId}")
    public Boolean addCompany(@PathVariable Integer hrId,@RequestBody Company company){
        Boolean aBoolean = companyService.addCompany(hrId, company);
        return aBoolean;
    }

    //更新
    @RequestMapping("/updateCompany")
    public Boolean updateCompany(@RequestBody Company Company){
        QueryWrapper<Company> wrapper=new QueryWrapper<>();
        wrapper.eq("CompanyId", Company.getCompanyId());
        Boolean update = companyService.update(Company,wrapper);
        return update;
    }

    //根据CompanyId查找公司
    @RequestMapping("/selCompanyById/{companyId}")
    public ResultVO selCompanyById(@PathVariable Integer companyId){
        ResultVO<Company> resultVO=new ResultVO<>();
        if(companyId!=null){
            Company byId = companyService.getById(companyId);
            if(byId!=null){
                resultVO.setMessage("succ");
                resultVO.setObject(byId);
            }else {
                resultVO.setMessage("error");
            }
        }
        return resultVO;
    }

//    更具userId查找公司
    @RequestMapping("/selCompanyByhrID/{hrId}")
    public ResultVO<Company> selCompanyByhrID(@PathVariable Integer hrId){
        ResultVO<Company> resultVO=new ResultVO<>();
        try {
            if(hrId!=null){
                Company company = companyService.selCompanyByhrId(hrId);
                if(company!=null){
                    resultVO.setMessage("succ");
                    resultVO.setObject(company);
                }else {
                    resultVO.setMessage("null");
                }
            }
        }catch (Exception e){
            resultVO.setMessage("error:"+e);
        }

        return resultVO;
    }

    //管理员查看Company列表
    @RequestMapping("/selCompanyList/{page}")
    public PageInfo<Company> selectPosition(@PathVariable Integer page, @RequestBody Company Company){
        PageInfo<Company> positions = companyService.selCompanyPage(page, Company);
        return positions;
    }

    //Hr绑定Company时通过名字查询
    @RequestMapping("/selCompany")
    public List<Company> selCompany( @RequestBody Company Company){
        List<Company> companies = companyService.selCompany(Company);
        return companies;
    }



    //上传公司图片
    @RequestMapping("/companyImage")
    public String headerImg(MultipartFile file){
        String s = null;
        try {
            s = uploadUtil.filesUpload(file, path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }
}
